//
//  CoreDefaultPickerView.m
//  AFNetworking
//
//  Created by abc on 2018/8/27.
//

#import "CoreDefaultPickerView.h"

#import <base/base.h>
#import <QMUIKit/QMUIKit.h>

@interface CoreDefaultPickerView () <UIPickerViewDelegate, UIPickerViewDelegate>
{
    NSString *_title;
    NSString *_selectValue;
    
    NSArray *_options;
    BOOL _isAutoSelect;
}

@property (nonatomic, strong) UIPickerView *pickerView;

@end

@implementation CoreDefaultPickerView

#pragma mark - 显示选择器
+ (CoreDefaultPickerView *)pickerViewWithTitle:(NSString *)title
                                    clearBlock:(ClearBlock)clearBlock
                                   resultBlock:(ResultBlock)resultBlock
{
    CoreDefaultPickerView *pickerView = [[CoreDefaultPickerView alloc] initWithTitle:title
                                                                          clearBlock:clearBlock
                                                                         resultBlock:resultBlock];
    return pickerView;
}

#pragma mark - 初始化选择器
- (instancetype)initWithTitle:(NSString *)title
                   clearBlock:(ClearBlock)clearBlock
                  resultBlock:(ResultBlock)resultBlock {
    if (self = [super init]) {
        _title = title;
        _selectValue = @"";
        _isAutoSelect = NO;
        
        _options = @[@"no data"];
        
        self.clearBlock = clearBlock;
        self.resultBlock = resultBlock;
        
        @weakify(self);
        self.dismissBlock = ^{
            @strongify(self);
            [self.pickerView removeFromSuperview];
            self.pickerView = nil;
        };
        
        [self initUI];
    }
    return self;
}

#pragma mark - 初始化子视图
- (void)initUI {
    [super initUI];
    self.titleLabel.text = _title;
    [self.alertView addSubview:self.pickerView];
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight)];
        
        _pickerView.backgroundColor = [UIColor whiteColor];
        _pickerView.tintColor = [UIColor clearColor];
        
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

#pragma mark - picker view delegate & datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _options.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [_options objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _selectValue = row<_options.count ? _options[row] : @"";
    if (_isAutoSelect && self.resultBlock) {
        self.resultBlock(_selectValue);
    }
}

#pragma mark - background view click event
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [self dismissWithAnimation:NO];
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
    if (self.clearBlock) {
        self.clearBlock();
    }
}

#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn {
    [self dismissWithAnimation:YES];
    if (self.resultBlock) {
        self.resultBlock(_selectValue);
    }
}

#pragma mark - 属性定制
- (CoreDefaultPickerView *(^)(NSArray *))options {
    return ^(NSArray *contain) {
        _options = contain;
        [self.pickerView reloadAllComponents];
        return self;
    };
}
- (CoreDefaultPickerView *(^)(BOOL autoSelect))autoSelect {
    return ^(BOOL autoSelect) {
        _isAutoSelect = autoSelect;
        return self;
    };
}
- (CoreDefaultPickerView *(^)(NSString *defaultSelect))selectValue {
    return ^(NSString *defaultSelect) {
        _selectValue = defaultSelect;
        return self;
    };
}
- (CoreDefaultPickerView *(^)())show {
    return ^() {
        if (_selectValue.length>0 && _options && [_options indexOfObject:_selectValue]!=NSNotFound)
        {
            [self.pickerView selectRow:[_options indexOfObject:_selectValue] inComponent:0 animated:NO];
        }
        else if (_selectValue.length<=0 && _options.count>0)
        {
            _selectValue = [_options objectAtIndex:0];
        }
        [self showWithAnimation:YES];
        return self;
    };
}

@end
